package com.DKCloudID.crypt;

import android.content.Context;
import android.util.Base64;

import com.dk.log.DKLog;
import com.dk.nfc.Tool.StringTool;

public class MsgCrypt {
    final static String TAG = "MsgCrypt";

    static {
        System.loadLibrary("dkcloudid_crypt");
    }

    public MsgCrypt() {

    }

    public MsgCrypt(Context context, String appId, String key) {
        try {
            byte[] keyBytes = Base64.decode(key.getBytes(), Base64.DEFAULT);
            byte[] appIdBytes = appId.getBytes();
            DKLog.d(TAG, "appid="+ StringTool.byteHexToSting(appIdBytes) + "key=" + StringTool.byteHexToSting(keyBytes));
            init(context, appIdBytes, keyBytes);
        }catch (Exception e) {
            DKLog.e(TAG, e.getMessage());
            DKLog.e(TAG, e.getStackTrace());
        }
    }

    //账号密码初始化
    public boolean msgCryptInit(Context context, String appId, String key) {
        try {
            byte[] keyBytes = Base64.decode(key.getBytes(), Base64.DEFAULT);
            byte[] appIdBytes = appId.getBytes();
            DKLog.d(TAG, "appid="+ StringTool.byteHexToSting(appIdBytes) + "key=" + StringTool.byteHexToSting(keyBytes));
            return init(context, appIdBytes, keyBytes);
        }catch (Exception e) {
            DKLog.e(TAG, e.getMessage());
            DKLog.e(TAG, e.getStackTrace());
            return false;
        }
    }

    public native boolean init(Context context, byte[] appIdBytes, byte[] keyBytes);
    public native byte[] getDnData();
    public native byte[] analyze(byte[] dnBytes);
    public native byte[] getDeviceId();
    public native byte[] getAppId();
}
